package liangtWorkSpace.integer;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName InClose
 * @Description : 演示反射
 * @Author : LiangT
 * @Date : 2020/8/31 12:03
 */
public class InClose {

    /**
     * 传统获取不通Map的方式
     * @param paeam
     * @return
     */
    public static Map<String,String> getMap(String paeam){
        Map<String,String> map = null;
        if(paeam.equals("HashMap")){
            map = new HashMap<>();
        }else if (paeam.equals("LinkedHashMap")){
            map = new LinkedHashMap<>();
        }
        return map;
    }

    /**
     * 利用反射获取不同的Map
     * @param className
     * @return
     * @throws ClassNotFoundException
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws InstantiationException
     */
    public static Map<String,String> getmap(String className) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class clazz = Class.forName(className);
        Constructor con = clazz.getConstructor();
        return (Map)con.newInstance();
    }
}
